package com.cy.aircompressor.main.home.systemsetting.fragment.child;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.RelativeLayout;

import androidx.core.content.ContextCompat;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.cy.aircompressor.R;
import com.cy.aircompressor.databinding.ChildContainerSecondFragmentBinding;
import com.cy.aircompressor.databinding.ChildDialogContainerBinding;
import com.cy.aircompressor.databinding.ExecutiveMaintenanceFootBinding;
import com.cy.aircompressor.databinding.PageTurningIndicatorContainerBinding;
import com.cy.aircompressor.databinding.SystemPowerManagerHeadBinding;
import com.cy.aircompressor.entity.power.PowerStation;
import com.cy.aircompressor.main.home.systemsetting.detail.PowerStationAddOrUpdateDialog;
import com.cy.aircompressor.main.home.systemsetting.provider.PowerManagerProvider;
import com.cy.aircompressor.main.home.systemsetting.viewmodel.SystemSettingViewModel;
import com.cy.aircompressor.main.view.PageTurningIndicator;
import com.cy.aircompressor.main.view.pop.PageSizePopupWindow;
import com.drug.lib.util.ToastUtil;
import com.drug.lib.view.recycptr.DynamicAdpTypePool;
import com.drug.lib.view.recycptr.DynamicRecyclerAdapter;
import com.drug.lib.view.recycptr.bean.RowEntity;
import com.jakewharton.rxbinding2.view.RxView;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.jakewharton.rxbinding2.widget.TextViewTextChangeEvent;
import com.net.model.base.Resource;
import com.net.model.base.fragment.BaseVMFragment;
import com.net.model.livedatas.LiveDataBus;
import com.net.model.utils.Constant;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;

public class PowerStationManagerFragment extends BaseVMFragment<ChildDialogContainerBinding, SystemSettingViewModel> {
    private Integer pageNum = 1;
    private Integer pageSize = 10;
    private Integer pageTotal = 1;
    private String name;

    public static PowerStationManagerFragment newInstance() {
        Bundle args = new Bundle();
        PowerStationManagerFragment fragment = new PowerStationManagerFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected void dataChange() {
        LiveDataBus.get("power_station", String.class).observe(this, new Observer<String>() {
            @Override
            public void onChanged(String jump) {
                requestNetwork();
            }

        });

        mViewModel.getQueryPowerStationByPage().observe(this, new Observer<Resource<RowEntity<PowerStation>>>() {
            @Override
            public void onChanged(Resource<RowEntity<PowerStation>> rowEntityResource) {
                rowEntityResource.handler(new OnCallback<RowEntity<PowerStation>>() {
                    @Override
                    public void onSuccess(RowEntity<PowerStation> data) {
                        footBinding.indicator.setRecordTotal(data.getTotal());
                        dynamicRecyclerAdapter.setItems(data.getList());
                        dynamicRecyclerAdapter.notifyDataSetChanged();
                        mBinding.nodata.dismiss();
                    }

                    @Override
                    public void onFailure(Integer code, String message) {
                        super.onFailure(code, message);
                        if (code == Constant.EMPTY_DATA_ERROR) {
                            dynamicRecyclerAdapter.setItems(new ArrayList<>());
                            dynamicRecyclerAdapter.notifyDataSetChanged();
                            mBinding.nodata.show();
                        }
                    }
                });
            }
        });
    }

    @Override
    protected ChildDialogContainerBinding getViewBinding() {
        return ChildDialogContainerBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initData() {
        headView();
        footView();
        initRecyclerAdapter();
    }

    @Override
    protected void requestNetwork() {
        mViewModel.queryPowerStationByPage(pageNum, pageSize, name);
    }

    SystemPowerManagerHeadBinding headBinding;

    private void headView() {
        headBinding = SystemPowerManagerHeadBinding.inflate(getLayoutInflater());
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        mBinding.airBaseHead.addView(headBinding.getRoot(), layoutParams);
        headBinding.addBtn.setText("+ 新增");
        RxView.clicks(headBinding.addBtn)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    PowerStationAddOrUpdateDialog.showDialogFragment(getChildFragmentManager(), "power_station", true, null);
                });
        RxView.clicks(headBinding.searchButton)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    requestNetwork();
                });
        RxTextView.textChangeEvents(headBinding.powerStationName)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<TextViewTextChangeEvent>() {
                    @Override
                    public void accept(TextViewTextChangeEvent textViewTextChangeEvent) throws Exception {
                        name = headBinding.powerStationName.getText().toString().trim();
                    }
                });
    }

    PageTurningIndicatorContainerBinding footBinding;

    private void footView() {
        footBinding = PageTurningIndicatorContainerBinding.inflate(getLayoutInflater());
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        mBinding.airBaseBottom.addView(footBinding.getRoot(), layoutParams);
        footBinding.indicator.setPageNum(pageNum);
        footBinding.indicator.setPageSize(pageSize);
        footBinding.indicator.setPageTurningIndicatorCallBack(new PageTurningIndicator.PageTurningIndicatorCallBack() {
            @Override
            public void skipPage(int page) {
                pageNum = page;
                requestNetwork();
            }

            @Override
            public void pageSize(int size) {
                pageSize = size;
                requestNetwork();
            }
        });
    }

    DynamicRecyclerAdapter dynamicRecyclerAdapter;

    private void initRecyclerAdapter() {
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext);
        mBinding.recyclerView.setLayoutManager(mLayoutManager);
        dynamicRecyclerAdapter = new DynamicRecyclerAdapter();
        DynamicAdpTypePool pool = new DynamicAdpTypePool();
        PowerManagerProvider powerManagerProvider = new PowerManagerProvider(mContext, getChildFragmentManager());
        pool.register(PowerStation.class, powerManagerProvider);
        dynamicRecyclerAdapter.registerAll(pool);
        mBinding.recyclerView.setAdapter(dynamicRecyclerAdapter);
    }
}
